<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Banner;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class BannerController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Banner(), function (Grid $grid) {
            $grid->scrollbarX();
            $grid->column('image_path')->image('', 300, 300);
            $grid->column('remark');
            $grid->column('enable')->switch();
            $grid->column('type')
                ->using(\App\Models\Banner::$typeList);
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });
            $grid->sortable();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Banner(), function (Show $show) {
            $show->field('id');
            $show->field('remark');
            $show->field('image_path');
            $show->field('enable');
            $show->field('order_n');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(Banner::indexQuery(), function (Form $form) {
            $form->display('id');
            $form->text('remark');

            $form->fieldset('图片/视频', function (Form $form) {
                $form->radio('type')
                    ->options(\App\Models\Banner::$typeList)
                    ->when([
                        \App\Models\Banner::IMAGE_TYPE,
                        \App\Models\Banner::VIDEO_TYPE
                    ], function (Form $form) {
                        $form->radio('radio-image', '图片上传方式')
                            ->when(2, function (Form $form) {
                                $form->file('image_path_upload', '上传图片')
                                    ->autoUpload()
                                    ->removable(false)
                                    ->uniqueName();
                            })
                            ->when(1, function (Form $form) {
                                $form->text('image_path', '图片')
                                    ->help('如果类型为图片,则显示图片.如果类型为视频,则显示为视频封面.');
                            })
                            ->options([
                                1 => '网络链接',
                                2 => '手动上传',
                            ])
                            ->value(1);

                    })
                    ->when([\App\Models\Banner::VIDEO_TYPE], function (Form $form) {
                        $form->radio('radio-video', '视频上传方式')
                            ->when(2, function (Form $form) {
                                $form->file('video_path_upload', '上传视频')
                                    ->autoUpload()
                                    ->removable(false)
                                    ->uniqueName();
                            })
                            ->when(1, function (Form $form) {
                                $form->text('video_path', '视频链接');
                            })
                            ->options([
                                1 => '网络链接',
                                2 => '手动上传',
                            ])
                            ->value(1);

                    })
                    ->required();
            });


            $form->switch('enable')->default(1);
            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form) {
                if ($upload = $form->input('image_path_upload')) {
                    $form->input('image_path', $upload);
                }
                if ($videoUpload = $form->input('video_path_upload')) {
                    $form->input('video_path', $videoUpload);
                }

                $form->deleteInput([
                    'radio-image',
                    'radio-video',
                    'image_path_upload',
                    'video_path_upload'
                ]);
            });
        });
    }
}
